package test;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import net.miginfocom.swing.MigLayout;

public class MigLayoutDemo {
	Container con;
	public MigLayoutDemo() {
		frame = new JFrame("MigLayout Demo");
		frame.setLayout(new GridLayout(2, 4));
		frame.setPreferredSize(new Dimension(700, 500));
		
		/*
		 * Column Constraints = [center][right][left][c]
		 * Row Constraints = [top][center][b]
		 */
		panel1 = new JPanel(new MigLayout("debug", "[][]20[]", "[]20[]")); // 1. Layout Constraints, 2. Column Constraints, 3. Row Constraints
		panel2 = new JPanel(new MigLayout("debug", "[]15[]", "[]15[]"));
		panel3 = new JPanel(new MigLayout("debug", "[]15[]", "[]15[]"));
		panel4 = new JPanel(new MigLayout("debug", "[]15[]", "[]15[]"));
		panel5 = new JPanel(new MigLayout("debug", "[]15[]", "[]15[]"));
		panel6 = new JPanel(new MigLayout("insets 0 10 10 20", "[]15[]", "[]15[]"));
		panel7 = new JPanel(new MigLayout("debug", "[]15[]", "[]15[]"));
		
		lbl1 = new JLabel("Label 1");
		lbl2 = new JLabel("Label 2");
		lbl3 = new JLabel("Label 3");
		lbl4 = new JLabel("Label 4");
		
		lbl11 = new JLabel("Label 1");
		lbl22 = new JLabel("Label 2");
		lbl33 = new JLabel("Label 3");
		lbl44 = new JLabel("Label 4");
		
		lbl111 = new JLabel("Label 1");
		lbl222 = new JLabel("Label 2");
		lbl333 = new JLabel("Label 3");
		lbl444 = new JLabel("Label 4");
		lbl555 = new JLabel("Label 5");
		lbl666 = new JLabel("Label 6");
		lbl777 = new JLabel("Label 7");
		
		lbl1111 = new JLabel("Label 1");
		lbl2222 = new JLabel("Label 2");
		lbl3333 = new JLabel("Label 3");
		lbl4444 = new JLabel("Label 4");
		lbl5555 = new JLabel("Label 5");
		
		lbl11111 = new JLabel("Label 1");
		lbl22222 = new JLabel("Label 2");
		lbl33333 = new JLabel("Label 3");
		lbl44444 = new JLabel("Label 4");
		
		lbl111111 = new JLabel("Label 1");
		lbl222222 = new JLabel("Label 2");
		lbl333333 = new JLabel("Label 3");
		lbl444444 = new JLabel("Label 4");
		
		comp1 = new JLabel("comp1");
		comp2 = new JLabel("comp2");
		comp3 = new JLabel("comp3");
		comp4 = new JLabel("comp4");
		comp1N = new JLabel("comp1N");
		comp2W = new JLabel("comp2W");
		comp3S = new JLabel("comp3S");
		comp4E = new JLabel("comp4E");
		
		/*
		 * Adding Components to the Grid
		 */
		panel1.add(lbl1);
		panel1.add(lbl2, "gapleft 50");
		panel1.add(lbl3, "wrap, w 40!"); // Wrap for next row
		panel1.add(lbl4);
		
		/*
		 * Merging and Splitting Cells 1
		 */
		panel2.add(lbl11, "width 10:20:40");
		panel2.add(lbl22, "span 2"); //The component will span two cells.
		panel2.add(lbl33, "wrap, height ::40"); // Wrap for next row
		panel2.add(lbl44, "span"); //Span without "count" means span whole row.
		
		/*
		 * Merging and Splitting Cells 2
		 */
		panel3.add(lbl111);
		panel3.add(lbl222, "span 2 2"); // The component will span 2x2 cells.
		panel3.add(lbl333, "wrap"); // Wrap for next row
		panel3.add(lbl444); //Span without "count" means span whole row.
		panel3.add(lbl555, "wrap"); // Note that it "jumps over" the occupied cells.
		panel3.add(lbl666);
		panel3.add(lbl777);
		
		/*
		 * Merging and Splitting Cells 3
		 */
		panel4.add(lbl1111);
		panel4.add(lbl2222, "split 2"); // Split the cell in two
		panel4.add(lbl3333); // Will be in same cell as previous
		panel4.add(lbl4444, "wrap"); // Wrap to next row
		panel4.add(lbl5555);
		
		/*
		 * Using Absolute Cell Coordinates 1
		 */
		panel5.add(lbl11111, "cell 0 0"); // "cell column row"
		panel5.add(lbl22222, "cell 1 0");
		panel5.add(lbl33333, "cell 2 0");
		panel5.add(lbl44444, "cell 0 1");
		
		/*
		 * Using Absolute Cell Coordinates 2
		 */
		panel6.add(lbl111111, "cell 0 0");
		panel6.add(lbl222222, "cell 1 0 2 1"); // "cell column row width height"
		panel6.add(lbl333333, "cell 3 0");
		panel6.add(lbl444444, "cell 0 1 4 1");
		
		/*
		 * Docking Components
		 */
		panel7.add(comp1);
		panel7.add(comp2);
		panel7.add(comp3, "wrap"); // Wrap to next row
		panel7.add(comp4);
		panel7.add(comp1N, "dock north");
		panel7.add(comp2W, "dock west");
		panel7.add(comp3S, "dock south");
		panel7.add(comp4E, "east"); // "dock" keyword are actually optional
		
		frame.add(panel1);
		frame.add(panel2);
		frame.add(panel3);
		frame.add(panel4);
		frame.add(panel5);
		frame.add(panel6);
		frame.add(panel7);
		MigLayoutDemo.setStyle(frame);
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}
	
	public static void setStyle(Container con) {
		Font font = new Font("Philosopher", Font.PLAIN, 18);
		for(Component C: con.getComponents()){
			if(C instanceof JLabel){
				JLabel lbl = (JLabel) C;
				lbl.setFont(font);
				lbl.setPreferredSize(new Dimension(100, 30));
			}else if(C instanceof Container){
				setStyle((Container)C);
			}
		}
	}
	
	public static void main(String [] args) {
		SwingUtilities.invokeLater(new Runnable() {			
			@Override
			public void run() {
				new MigLayoutDemo();
			}
		});
	}
	
	/*
	 * @variable
	 */
	JLabel lbl1, lbl2, lbl3, lbl4;
	JLabel lbl11, lbl22, lbl33, lbl44;
	JLabel lbl11111, lbl22222, lbl33333, lbl44444;
	JLabel lbl111111, lbl222222, lbl333333, lbl444444;
	JLabel lbl1111, lbl2222, lbl3333, lbl4444, lbl5555;
	JLabel lbl111, lbl222, lbl333, lbl444, lbl555, lbl666, lbl777;
	JLabel comp1, comp2, comp3, comp4, comp1N, comp2W, comp3S, comp4E;
	JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7;
	static JFrame frame;
}
